package com.sheng.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sheng.entity.UserEntity;
import com.sheng.entity.UserTokenEntity;
import com.sheng.form.LoginRequest;
import com.sheng.form.UserQueryParams;
import com.sheng.model.UserModel;
import com.sheng.service.UserService;
import com.sheng.utils.Assert;

/**
 *   用户控制类
 *   @RestController 注解标明UserController是一个控制类(Controller)
 */
@RestController
public class UserController {
	
//	@Autowired //spring 自动注入
	@Reference
	private UserService userService;

	/**
	 post http://localhost:8080/fulu/user/add
	 json 对象
	 {
	  	"userNo":"NO000001",
		"userName":"王大锤",
		"password":"123321",
		"phone":"10086",
		"userType":"admin",
		"sex":"male"
	  }
	 */
	
	/**
	 * 新增用户
	 * @param user 用户对象数据
	 * @throws Exception 
	 */
	@RequestMapping("/user/add") //路由地址 /user/add
	public void add(@RequestBody UserModel user) throws Exception{
		/**
		 *  第一种数据校验
			 if(null==user.getUserNo()||"".equals(user.getUserNo().trim())) {
				throw new Exception("用户编号不能空");
			}
			if(null==user.getUserName()||"".equals(user.getUserName().trim())) {
				throw new Exception("用户名不能空");
			}
			if(null==user.getPassword()||"".equals(user.getPassword().trim())) {
				throw new Exception("密码不能空");
			}
			if(null==user.getPhone()||"".equals(user.getPhone().trim())) {
				throw new Exception("手机号码不能空");
			}
			if(null==user.getUserType()||"".equals(user.getUserType().trim())) {
				throw new Exception("用户类型不能空");
			}
		 */
		
		/**
		 * 第二种数据校验
		 if(StringUtils.isEmpty(user.getUserNo())) {
			throw new Exception("用户编号不能空");
		 }
		 */
		
		/**
		 * 第三种方法
		 
		 */
		Assert.notBlank(user.getUserNo(), "用户编号不能空");
		
		
		System.out.println("接收前端请求");
		userService.createUser(user);
	}
	
	/**http://localhost:8080/fulu/user/2/delete
	 * 删除用户
	 * @param userId 用户id
	 */
	@RequestMapping("/user/{userId}/delete") //路由上的参数
	public void delete(@PathVariable Integer userId){
		System.out.println("delete user ....");
		userService.removeUser(userId);
	}
	
	/**
	 * 更新用户
	 * @param user 用户对象数据
	 * @throws Exception 
	 */
	@RequestMapping("/user/{userId}/update")// /user/1/update
	public void update(HttpServletRequest httpServletRequest,@RequestBody UserEntity user,@PathVariable Integer userId) throws Exception{
		System.out.println("update user ...");
		
		user.setUserId(userId);
		userService.updateUser(user);
	}
//	public 返回类型 方法名(参数...)
	@RequestMapping("/user/{userId}/get")
	public UserModel qqqq(@PathVariable Integer userId){
		
		UserModel userEntity = userService.getUser(userId);
		return userEntity;
	}
	
	@RequestMapping("/user/list")
	public List<UserModel> query(@RequestBody UserQueryParams userQuery){
		
		return userService.findUserList(userQuery);
	}
	
	
	@RequestMapping("/user/login") //路由地址 /user/add
	public UserTokenEntity login(HttpServletRequest httpServletRequest,@RequestBody LoginRequest request) throws Exception{
		
		String userNo = request.getUserNo();
		String password = request.getPassword();
		
		UserTokenEntity user = this.userService.login(userNo,password);
		return user;
	}
	
	
}
